package com.aerospace.example.helloworld.service.impl;

import com.weibo.example.hello.GreeterGrpc;
import com.weibo.example.hello.HelloReply;
import com.weibo.example.hello.HelloRequest;
import com.weibo.example.hello.service.HelloServerService;
import io.grpc.stub.StreamObserver;

/**
 * @ClassName HelloServerServiceImpl
 * @Description
 * @Author yuhang11
 * @Date 2021/7/12 5:36 下午
 * @Version 1.0
 */
public class HelloServerServiceImpl extends GreeterGrpc.GreeterImplBase implements HelloServerService   {
    @Override
    public void simpleHello(HelloRequest request, StreamObserver<HelloReply> responseObserver) {
        HelloReply reply = HelloReply.newBuilder().setMessage("Hello" + request.getName()).build();
        responseObserver.onNext(reply);
        responseObserver.onCompleted();
    }

    @Override
    public void serverStreamHello(HelloRequest request, StreamObserver<HelloReply> responseObserver) {
        responseObserver.onNext(HelloReply.newBuilder().setMessage("hello1,"+request.getName()).build());
        responseObserver.onNext(HelloReply.newBuilder().setMessage("hello2,"+request.getName()).build());
        responseObserver.onNext(HelloReply.newBuilder().setMessage("hello3,"+request.getName()).build());
        responseObserver.onCompleted();
    }

    @Override
    public StreamObserver<HelloRequest> clientStreamHello(final StreamObserver<HelloReply> responseObserver) {
        return new StreamObserver<HelloRequest>() {
            private HelloReply.Builder builder = HelloReply.newBuilder();
            @Override
            public void onNext(HelloRequest helloRequest) {
                builder.setMessage(builder.getMessage()+","+helloRequest.getName());
            }

            @Override
            public void onError(Throwable throwable) {

            }

            @Override
            public void onCompleted() {
                builder.setMessage("hello"+builder.getMessage());
                responseObserver.onCompleted();
            }
        };
    }

    @Override
    public StreamObserver<HelloRequest> biStreamHello(final StreamObserver<HelloReply> responseObserver) {
        return new StreamObserver<HelloRequest>() {
            @Override
            public void onNext(HelloRequest helloRequest) {
                responseObserver.onNext(HelloReply.newBuilder().setMessage("hello1,"+helloRequest.getName()).build());
                responseObserver.onNext(HelloReply.newBuilder().setMessage("hello2,"+helloRequest.getName()).build());
                responseObserver.onNext(HelloReply.newBuilder().setMessage("hello3,"+helloRequest.getName()).build());
            }

            @Override
            public void onError(Throwable throwable) {

            }

            @Override
            public void onCompleted() {
                responseObserver.onCompleted();
            }
        };
    }
}
